import 'package:flustars/flustars.dart';
import 'package:flutter/material.dart';
import 'package:yuan_hang_app/base/base_state.dart';
import 'package:yuan_hang_app/base/full_base_page.dart';
import 'package:yuan_hang_app/http/api.dart';
import 'package:yuan_hang_app/http/http_utils.dart';
import 'package:yuan_hang_app/res/w_colors.dart';
import 'package:yuan_hang_app/res/w_constant.dart';
import 'package:yuan_hang_app/res/w_dimens.dart';
import 'package:yuan_hang_app/utils/common_req_utils.dart';
import 'package:yuan_hang_app/utils/jmessage_helper.dart';
import 'package:yuan_hang_app/utils/router_helper.dart';
import 'package:yuan_hang_app/utils/size_utils.dart';
import 'package:yuan_hang_app/utils/string_utils.dart';
import 'package:yuan_hang_app/views/enter/view/login_page.dart';
import 'package:yuan_hang_app/widget/label_item.dart';
import 'package:yuan_hang_app/widget/status_btn.dart';
import 'package:yuan_hang_app/widget/w_line.dart';
import 'package:yuan_hang_app/widget/w_text.dart';
import 'package:yuan_hang_app/widget/w_toast.dart';
import 'package:yuan_hang_app/widget/water_mark.dart';

class EditPassPage extends StatefulWidget {
  final bool? isForceChange;

  const EditPassPage({super.key, this.isForceChange = false});

  @override
  BaseState<EditPassPage> createState() => _EditPassPageState();
}

class _EditPassPageState extends BaseState<EditPassPage> {
  String _password = "";
  String _newPassword = "";
  String _confromPasswod = "";
  bool canSeePass = false;

  @override
  FullBasePage buildState(BuildContext context) {
    return FullBasePage(
        leftWidget:
            widget.isForceChange == true ? const SizedBox.shrink() : null,
        title: "修改密码",
        child: Column(
          children: [
            WLine(width: 0.5.px),
            LabelItem(
              label: "原密码",
              isMust: true,
              labelType: LabelType.INPUT,
              isPassword: true,
              onChanged: (text) {
                _password = text;
              },
            ),
            LabelItem(
              label: "新密码",
              isMust: true,
              labelType: LabelType.INPUT,
              isPassword: true,
              onChanged: (text) {
                _newPassword = text;
              },
            ),
            LabelItem(
              label: "再次输入新密码",
              isMust: true,
              labelType: LabelType.INPUT,

              isPassword: true,
              onChanged: (text) {
                _confromPasswod = text;
              },
            ),
            Padding(
              padding: EdgeInsets.symmetric(vertical: 20.px, horizontal: 30.px),
              child: Row(
                mainAxisAlignment: MainAxisAlignment.center,
                children: [
                  if (widget.isForceChange == false)
                    Expanded(
                        child: StatusBtn(
                      text: "取消",
                      isTag: true,
                      isItemBtn: true,
                      height: 48.px,
                      fontSize: WDimens.tsLabel,
                      onPressed: () {
                        Navigator.pop(context);
                      },
                    )),
                  if (widget.isForceChange == false)
                    SizedBox(
                      width: 20.px,
                    ),
                  Expanded(
                    child: StatusBtn(
                      text: "提交",
                      isTag: true,
                      isItemBtn: true,
                      height: 48.px,
                      fontSize: WDimens.tsLabel,
                      onPressed: () {
                        if (_password == "") {
                          Toast.toast("请输入原密码");
                          return;
                        }
                        if (_newPassword == "") {
                          Toast.toast("请输入新密码");
                          return;
                        }
                        if (_confromPasswod == "") {
                          Toast.toast("确认的新密码");
                          return;
                        }
                        if (_newPassword == _password) {
                          Toast.toast("原密码和新密码输入相同");
                          return;
                        }
                        if (!StringUtils.validatePassword(_newPassword)) {
                          Toast.toast("请输入8-32个字符,至少包含数字、大写字母、小写字母、特殊字符中的三种类型");
                          return;
                        }
                        if (_newPassword != _confromPasswod) {
                          Toast.toast("两次密码输入不一致");
                          return;
                        }
                        editPassword();
                      },
                    ),
                  ),
                ],
              ),
            )
          ],
        ));
  }

  void editPassword() {
    Map<String, dynamic> params = {
      "password": _password,
      "newPassword": _newPassword
    };

    showLoading();
    HttpUtils().post(Api.editPassword, data: params).then((val) {
      hideLoading();
      if (val.resultCode == 200) {
        Toast.toast("修改成功！");
        Toast.showLoading();
        String username = SpUtil.getString(WConstant.username) ?? "";
        JmessageHelper.removeAlias();
        SpUtil.clear()?.then((value) {
          Toast.dismissLoading();
          SpUtil.putString(WConstant.username, username);
          WConstant.navigatorKey.currentState?.pushNamed("/loginPage");
        });
        // String token = SpUtil.getString(WConstant.token) ?? "";
        // Map<String, dynamic> params = {
        //   "jwt": token,
        // };
        // HttpUtils().get(Api.loginOut, queryParams: params).then((value) {
        //   SpUtil.clear()?.then((value) {
        //
        //     RouteHelper.pushReplacement(context, const LoginPage());
        //   });
        // });
      } else if (val.resultCode == 400) {
        Toast.toast(val.errMsg);
      }
    });
  }
}
